package com.cwf.common.core.controller;


import com.cwf.common.entity.auth.TokenInfo;
import com.cwf.common.enums.ResCodeEnum;
import com.cwf.common.enums.TokenInfoKeyEnum;
import com.cwf.common.exception.base.MyException;
import com.cwf.common.utils.auth.JwtUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
@RestController
@Slf4j
public class BaseController {

    @Resource
    private HttpServletRequest req;


    /**
     * @return
     * @description 获取HttpServletRequest
     * @author 18068
     * @date 2023/2/15 14:57
     */
    public HttpServletRequest getHttpServletRequest() {
        HttpServletRequest httpServletRequest = req;
        return httpServletRequest;
    }


    /**
     * @return
     * @description 获取请求头值
     * @author 18068
     * @date 2023/2/15 14:57
     */
    public String getHeaderValue(String key) {
        String header = req.getHeader(key);
        return header;
    }


    /**
     * @description 获取token
     * @author 18068
     * @date 2023/2/15 14:36
     */
    public String getToken() {

        return req.getHeader("Authorization");
    }


    /**
     * @param key key值
     * @return
     * @description 获取token
     * @author 18068
     * @date 2023/2/15 14:36
     */
    public String getToken(String key) {
        return req.getHeader(key);
    }


    /**
     * @return
     * @description 从token获取用户id
     * @author 18068
     * @date 2023/2/15 14:39
     */
    public String getUserId() {

        TokenInfo tokenInfo = JwtUtil.getTokenInfo(getToken(), TokenInfoKeyEnum.OK.getCode());

        return tokenInfo.getUserId();
    }


    /**
     * @return
     * @description 从token获取用户信息
     * @author 18068
     * @date 2023/2/15 14:39
     */
    public TokenInfo getUserInfo() {
        TokenInfo tokenInfo = JwtUtil.getTokenInfo(getToken(), TokenInfoKeyEnum.OK.getCode());
        return tokenInfo;
    }


    public void pageStart() {

        String page = req.getParameter("page");
        String size = req.getParameter("size");

        if (!StringUtils.hasLength(page)) {
            page = "1";
        }

        if (!StringUtils.hasLength(size)) {
            size = "5";
        }

        if (!StringUtils.hasLength(page) && !StringUtils.hasLength(size)) {
            page = "1";
            size = "5";
        }
        log.info("\npage: {} \nsize: {}", page, size);

        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(size));
        PageHelper.clearPage();

    }




    public void isEmpty(Object obj){
        Class<?> aClass = obj.getClass();

        Field[] declaredFields = aClass.getDeclaredFields();


        boolean isAllNull = true;

        String isEmptyStr = "";

        for (Field field : declaredFields){
            field.setAccessible(true);
            try {
                Object value = field.get(obj);
                String s = Modifier.toString(field.getModifiers());

                if (!s.equals("private static final")){
                    if (value!=null){
                        isAllNull = false;

                        if (value.equals("")){
                            String name = field.getName()+"为无效空字符;";
                            isEmptyStr+=name;
                        }
                    }
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        if (isEmptyStr.length()!=0){
            throw new MyException(isEmptyStr, ResCodeEnum.PARAM_ERR.getCode());
        }

        if (isAllNull){
            throw new MyException("无效参数", ResCodeEnum.PARAM_ERR.getCode());
        }


    }


}
